import sklearn
import pandas as pd

df = pd.read_csv("E:/ai/score.txt", sep='\t')
print("特征数量：\n", df.shape)

data = df.dropna()

# 查看数据
print(data.head())

# 查看数据统计信息
print(data.describe())

import matplotlib.pyplot as plt
import seaborn as sns

"""
# 绘制房价分布图
sns.histplot(data['rs_zf'], kde=True, bins=20)
plt.title("rs_zf Distribution")  # 房价分布
plt.xlabel("rs_zf")  # 房价
plt.ylabel("Frequency")  # 频数
plt.show()

# 绘制特征相关性热力图
plt.figure(figsize=(10, 8))
sns.heatmap(data.corr(), annot=True, cmap='coolwarm')
plt.title("Feature Correlation Heatmap")  # 特征相关性热力图
plt.show()
"""

# 检查缺失值
print(data.isnull().sum())

from sklearn.preprocessing import StandardScaler

# 特征标准化
scaler = StandardScaler()
features = data.drop('rs_zf', axis=1)
target = data['rs_zf']
features_scaled = scaler.fit_transform(features)

from sklearn.model_selection import train_test_split

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(features_scaled, target, test_size=0.2, random_state=42)

from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# 初始化模型
model = LinearRegression()

# 训练模型
model.fit(X_train, y_train)

# 预测
y_pred = model.predict(X_test)

# 评估性能
mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)
print(f"均方误差（MSE）：{mse}")
print(f"R2 分数：{r2}")

import tensorflow as tf
from keras.models import Sequential
from keras.layers import Dense, Input

# 定义模型
nn_model = Sequential([
    Input(shape=(X_train.shape[1],)),  # 显式定义输入层
    Dense(64, activation='relu'),  # 第一层隐藏层
    Dense(32, activation='relu'),  # 第二层隐藏层
    Dense(1)  # 输出层，预测房价
])

# 编译模型
nn_model.compile(optimizer='adam', loss='mse', metrics=['mae'])

# 打印模型结构
nn_model.summary()
